package it.koutifaris.travianmap.mapsqlparser;

import static it.koutifaris.testforclassutils.IsTestForAssert.assertIsTestFor;
import it.koutifaris.travianmap.TestRemoteSource;

import java.io.IOException;
import java.util.Date;

import junit.framework.Assert;
import android.test.AndroidTestCase;

public class RegexMapSqlParserTest extends AndroidTestCase {

	private RegexMapSqlParser parser;
	private MapSqlParser.RemoteSource testMapSource;

	@Override
	public void setUp() throws Exception {
		testMapSource = new TestRemoteSource(new Date(), getClass().getResourceAsStream("map.sql.gz"), true);
		parser = new RegexMapSqlParser();
	}

	public void testPkgAndName() {
		assertIsTestFor(RegexMapSqlParser.class);
	}

	public void test() throws Exception {
		String line = "INSERT INTO `x_world` VALUES (754,353,400,2,120516,'-R2-Susina',10852,'nathan',1347,'SOLI',25);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(754);
		expected.setX(353);
		expected.setY(400);
		expected.setTribeId(2);
		expected.setVillageId(120516);
		expected.setVillageName("-R2-Susina");
		expected.setPlayerId(10852);
		expected.setPlayerName("nathan");
		expected.setAllianceId(1347);
		expected.setAllianceName("SOLI");
		expected.setVillagePopulation(25);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithEmpty() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA',5293,'JULIUS',0,'',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA");
		expected.setPlayerId(5293);
		expected.setPlayerName("JULIUS");
		expected.setAllianceId(0);
		expected.setAllianceName("");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithQuotes() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA',5293,'JUL&#39;IUS',0,'',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA");
		expected.setPlayerId(5293);
		expected.setPlayerName("JUL'IUS");
		expected.setAllianceId(0);
		expected.setAllianceName("");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testLineWithDoubleQuotes() throws IOException, Exception {
		String line = "INSERT INTO `x_world` VALUES (801,400,400,1,106555,'ROMA',5293,'JUL\"IUS',0,'',551);";
		final MapSqlEntry expected = new MapSqlEntry();
		expected.setId(801);
		expected.setX(400);
		expected.setY(400);
		expected.setTribeId(1);
		expected.setVillageId(106555);
		expected.setVillageName("ROMA");
		expected.setPlayerId(5293);
		expected.setPlayerName("JUL\"IUS");
		expected.setAllianceId(0);
		expected.setAllianceName("");
		expected.setVillagePopulation(551);

		MapSqlEntry actual = parser.parseLine(line);

		Assert.assertEquals(expected, actual);
	}

	public void testParseAll() throws IOException, Exception {
		parser.parse(testMapSource, new MapSqlEntryHandler() {

			boolean initCalled = false;
			boolean onEntryCalled = false;

			@Override
			public boolean onEntry(MapSqlEntry actual) {
				onEntryCalled = true;
				return true;
			}

			@Override
			public void init(Date lastModidied) {
				initCalled = true;
			}

			@Override
			public void done() {
				Assert.assertTrue(onEntryCalled && initCalled);
			}

			@Override
			public void onFail(Exception e) {
				Assert.fail(e.getMessage());
			}
		});
	}
}
